package com.drinks.yiniang.adapter.goods;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.ui.widget.textview.TagTextView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.ui.widget.layout.AutoLineFeedLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

/**
 * 推荐商品列表adapter
 * @author han
 * @date 2019/8/30
 */
public class MyCommentsAdapter extends BaseQuickRCVAdapter<GoodsJson.GoodsList, BaseViewHolder> {

    private TagTextView mTagTextView;

    private StringBuilder mBuilder;

    public MyCommentsAdapter(Context mContext, @Nullable List<GoodsJson.GoodsList> data) {
        super(R.layout.item_home_goods, data);
        this.mContext = mContext;
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsJson.GoodsList item) {
        GlideUtils.loadGoodsList(mContext, (RatioRoundImageView) helper.getView(R.id.iv_home_goods_cover), item.getCover_url());

        //活动tag
        addLabelTag((AutoLineFeedLayout) helper.getView(R.id.ll_home_goods_label), item.getActivity());

        //商品名称
        helper.setText(R.id.tv_home_goods_name, item.getName());
        //推荐语
        helper.setText(R.id.tv_home_goods_slogan, item.getRecommend_reason());
        //价格
        mBuilder = new StringBuilder();
        mBuilder.append("¥");
        mBuilder.append(StringUtils.getGoodsPrice(item.getApp_price(), item.getActivity_price()));
        helper.setText(R.id.tv_home_goods_price, mBuilder);

        helper.addOnClickListener(R.id.iv_home_goods_add_cart);
    }

    private void addLabelTag(AutoLineFeedLayout layout, List<GoodsJson.GoodsActivity> actList) {
        layout.removeAllViews();

        if (CommonUtils.isEmptyList(actList)) {
            layout.setVisibility(View.GONE);

            return;
        }

        layout.setVisibility(View.VISIBLE);

        for (GoodsJson.GoodsActivity data : actList) {
            mTagTextView = new TagTextView(mContext);

            mTagTextView.setTagColor(data.getTag_color());
            mTagTextView.setText(data.getTag_name());

            layout.addView(mTagTextView);
        }
    }
}
